package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.place.PlaceDto;
import com.lnking.lnkingplace.model.entity.PlaceInfo;
import com.lnking.lnkingplace.model.entity.RecordPlace;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.record.place.RecordPlaceAddRequest;
import com.lnking.lnkingplace.model.request.record.place.RecordPlaceSearchRequest;
import com.lnking.lnkingplace.model.request.record.place.RecordPlaceUpdateRequest;
import com.lnking.lnkingplace.service.IPlaceInfoService;
import com.lnking.lnkingplace.service.IRecordPlaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-19
 */
@RestController
@RequestMapping("/record-place")
@Api(tags = "场所防疫")
public class RecordPlaceController {

    @Resource
    IRecordPlaceService recordPlaceService;

    @Resource
    IPlaceInfoService placeInfoService;
    @Resource
    UserUtil userUtil;

    @ApiOperation("查询列表")
    @GetMapping("/list")
    public ReturnResult getList(RecordPlaceSearchRequest params) {
        String type = userUtil.getUser().getType();
        //隔离场所
        if(type.equals(UserType.PLACE)){
            params.setPlaceName(userUtil.getUser().getName());
        }
        return ReturnResult.success(recordPlaceService.pageList(params));
    }

    @ApiOperation("新增")
    @PostMapping("/add")
    public ReturnResult add(@RequestBody @Valid RecordPlaceAddRequest params) throws Exception {
        recordPlaceService.add(params);
        return ReturnResult.success();
    }

    @ApiOperation("编辑")
    @PostMapping("/update")
    public ReturnResult update(@RequestBody @Valid RecordPlaceUpdateRequest params) throws Exception {
        recordPlaceService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        recordPlaceService.removeById(id);
        return ReturnResult.success();
    }

    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id){
        //获取防疫信息
        RecordPlace recordPlace = recordPlaceService.getById(id);
        PlaceInfo placeInfo = placeInfoService.getDetails(recordPlace.getPlaceId());
        PlaceDto placeDTO = new PlaceDto();
        BeanUtils.copyProperties(placeInfo, placeDTO);
        placeDTO.setInfo(recordPlace.getInfo());
        placeDTO.setType(recordPlace.getType());
        return ReturnResult.success(placeDTO);
    }
}
